<?php
/**
 * 文章数据模型
*
* @author sandy
*
*/
class articleModel extends Model{
	
	/**
	 * 获取指定图书的所有文章
	 * 
	 * @param int $book_id
	 * @param array $page_info
	 */
	public function get_book_article($book_id,$page_info = array()){
		
		if(!$book_id){
			return false;	
		}
		$return = array('result'=>'','total'=>0);
		
		$sql = "SELECT * FROM #__article WHERE book_id = ".$book_id;
		$sql_count = "SELECT count(*) FROM #__article WHERE book_id = ".$book_id;
		
		if (!empty($page_info)){
			$this->db->set_query($sql,$page_info['offset'],$page_info['limit']);
		} else {
			$this->db->set_query($sql);
		}
		
		$return['result'] = $this->db->get_all();
		
		$this->db->set_query($sql_count);
		$return['total'] = $this->db->get_one();
		
		return $return;
	}
	
	/**
	 * 获取指定章节的所有文章
	 * 
	 * @param int $chapter_id
	 * @param array $page_info
	 */
	public function get_chapter_article($chapter_id,$page_info = array()){
		if(!$chapter_id){
			return false;	
		}
		$return = array('result'=>'','total'=>0);
		
		$sql = "SELECT * FROM #__article WHERE chapter_id = ".$chapter_id;
		$sql_count = "SELECT count(*) FROM #__article WHERE book_id = ".$chapter_id;
		
		if (!empty($page_info)){
			$this->db->set_query($sql,$page_info['offset'],$page_info['limit']);
		} else {
			$this->db->set_query($sql);
		}
		
		$return['result'] = $this->db->get_all();
		
		$this->db->set_query($sql_count);
		$return['total'] = $this->db->get_one();
		
		return $return;
	}
	
	/**
	 * 获取指定文件信息
	 * @param int $article_id
	 */
	public function get_article_info($article_id){
		$article_id = intval($article_id);
		
		if(!$article_id){
			return false;
		}
		
		$sql = "SELECT * FROM #__article WHERE article_id = $article_id";
		
		$this->db->set_query($sql);
		
		return $this->db->get_row();
	}
	
	
	/**
	 * 添加文章
	 * @param array $article_info
	 */
	public function add_article($article_info){
		$insert_info = array(
				'article_title' => $article_info['article_title'],
				'book_id' => $article_info['book_id'],
				'chapter_id' => $article_info['chapter_id'],
				'content' => $article_info['content'],
				'addtime' => time(),
				'lastmodify ' => time()
		);
		
		return $this->db->insert("#__article",$insert_info);
	}
	
	public function update_article($article_info){
		
		$update_info = array(
				'article_title' => $article_info['article_title'],
				'content' => $article_info['content'],
				'lastmodify' => time()
		);
		$where = "article_id = " . $article_info['article_id'];
		
		return $this->db->update("#__article",$update_info,$where);
	}
	
	/**
	 * 删除指定文章
	 * @param int $article_id
	 */
	public function del_article($article_id){
		
		$article_id = intval($article_id);
		if (!$article_id){
			return false;
		}
		
		$sql = "DELETE FROM #__article WHERE article_id = ".$article_id;
		$this->db->set_query($sql);
		
		return $this->db->query();
	}
}

